package org.linlinjava.litemall.db.service;

import java.time.LocalDateTime;
import java.util.List;

import javax.annotation.Resource;

import org.beetl.sql.core.query.LambdaQuery;
import org.linlinjava.litemall.db.domain.LitemallComment;
import org.linlinjava.litemall.db.mapper.LitemallCommentDao;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class LitemallCommentService {
    @Resource
    private LitemallCommentDao commentDao;

    public List<LitemallComment> queryGoodsByGid(Integer id, int offset, int limit) {
    	LambdaQuery<LitemallComment> query = commentDao.createLambdaQuery();
    	query.andEq(LitemallComment::getValueId, id);
    	query.andEq(LitemallComment::getDeleted, false);
    	query.andEq(LitemallComment::getType, (byte)0);
    	
    	query.orderBy(LitemallComment.Column.addTime.desc());
    	
    	query.limit(offset, limit);
    	List<LitemallComment> list = query.select();
    	return list;
		/*
		 * LitemallCommentExample example = new LitemallCommentExample();
		 * example.setOrderByClause(LitemallComment.Column.addTime.desc());
		 * example.or().andValueIdEqualTo(id).andTypeEqualTo((byte)
		 * 0).andDeletedEqualTo(false); PageHelper.startPage(offset, limit); return
		 * commentMapper.selectByExample(example);
		 */
    }

    public List<LitemallComment> query(Byte type, Integer valueId, Integer showType, Integer offset, Integer limit) {
    	
    	LambdaQuery<LitemallComment> query = commentDao.createLambdaQuery();
    	if(showType == 0 ) {
    		query.andEq(LitemallComment::getValueId, valueId);
        	query.andEq(LitemallComment::getDeleted, false);
        	query.andEq(LitemallComment::getType,type);
    	}else if(showType   == 1 ) {
    		query.andEq(LitemallComment::getValueId, valueId);
        	query.andEq(LitemallComment::getDeleted, false);
        	query.andEq(LitemallComment::getHasPicture, true);
        	query.andEq(LitemallComment::getType,type);
    	}else {
    		 throw new RuntimeException("showType不支持");
    	}
    	query.orderBy(LitemallComment.Column.addTime.desc());
    	
    	query.limit(offset, limit);
    	List<LitemallComment> list = query.select();
    	return list;
    	
       /* LitemallCommentExample example = new LitemallCommentExample();
        example.setOrderByClause(LitemallComment.Column.addTime.desc());
        if (showType == 0) {
            example.or().andValueIdEqualTo(valueId).andTypeEqualTo(type).andDeletedEqualTo(false);
        } else if (showType == 1) {
            example.or().andValueIdEqualTo(valueId).andTypeEqualTo(type).andHasPictureEqualTo(true).andDeletedEqualTo(false);
        } else {
            throw new RuntimeException("showType不支持");
        }
        PageHelper.startPage(offset, limit);
        return commentMapper.selectByExample(example);*/
    }

    public int count(Byte type, Integer valueId, Integer showType) {
    	LitemallComment comment = new LitemallComment();
    	if (showType == 0) {
    		comment.setValueId(valueId);
    		comment.setType(type);
    		comment.setDeleted(false);
        } else if (showType == 1) {
        	comment.setValueId(valueId);
    		comment.setType(type);
    		comment.setHasPicture(true);
    		comment.setDeleted(false);
        } else {
            throw new RuntimeException("showType不支持");
        }
    	
    	return (int)commentDao.templateCount(comment);
    	
		/*
		 * LitemallCommentExample example = new LitemallCommentExample(); if (showType
		 * == 0) { example.or().andValueIdEqualTo(valueId).andTypeEqualTo(type).
		 * andDeletedEqualTo(false); } else if (showType == 1) {
		 * example.or().andValueIdEqualTo(valueId).andTypeEqualTo(type).
		 * andHasPictureEqualTo(true).andDeletedEqualTo(false); } else { throw new
		 * RuntimeException("showType不支持"); } return (int)
		 * commentMapper.countByExample(example);
		 */
    }

    public int save(LitemallComment comment) {
        comment.setAddTime(LocalDateTime.now());
        comment.setUpdateTime(LocalDateTime.now());
        
        commentDao.insertTemplate(comment,true);
        return 1;
        //return commentMapper.insertSelective(comment);
    }

    public List<LitemallComment> querySelective(String userId, String valueId, Integer page, Integer size, String sort, String order) {
    	
    	LambdaQuery<LitemallComment> query = commentDao.createLambdaQuery();
    	
    	if (!StringUtils.isEmpty(userId)) {
    		query.andEq(LitemallComment::getUserId, Integer.valueOf(userId));
        }
        if (!StringUtils.isEmpty(valueId)) {
        	query.andEq(LitemallComment::getValueId, Integer.valueOf(valueId));
        	query.andEq(LitemallComment::getType, (byte) 0);
            //criteria.andValueIdEqualTo(Integer.valueOf(valueId)).andTypeEqualTo((byte) 0);
        }
        query.andEq(LitemallComment::getDeleted, false);
        
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
        	query.orderBy(sort + " " + order);
        }
    	query.limit(page, size);
    	List<LitemallComment> list = query.select();
    	return list;
    	
    	
    	
		/*
		 * LitemallCommentExample example = new LitemallCommentExample();
		 * LitemallCommentExample.Criteria criteria = example.createCriteria();
		 * 
		 * // type=2 是订单商品回复，这里过滤 criteria.andTypeNotEqualTo((byte) 2);
		 * 
		 * if (!StringUtils.isEmpty(userId)) {
		 * criteria.andUserIdEqualTo(Integer.valueOf(userId)); } if
		 * (!StringUtils.isEmpty(valueId)) {
		 * criteria.andValueIdEqualTo(Integer.valueOf(valueId)).andTypeEqualTo((byte)
		 * 0); } criteria.andDeletedEqualTo(false);
		 * 
		 * if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
		 * example.setOrderByClause(sort + " " + order); }
		 * 
		 * PageHelper.startPage(page, size); return
		 * commentMapper.selectByExample(example);
		 */
    }

    public void deleteById(Integer id) {
    	
    	LitemallComment comment = new LitemallComment();
    	comment.setId(id);
    	comment.setDeleted(true);
    	commentDao.updateTemplateById(comment);
        //commentMapper.logicalDeleteByPrimaryKey(id);
    }

    public LitemallComment findById(Integer id) {
    	return commentDao.single(id);
       // return commentMapper.selectByPrimaryKey(id);
    }

    public int updateById(LitemallComment comment) {
    	//comment.setUpdateTime(LocalDateTime.now());
    	return commentDao.updateTemplateById(comment);
        //return commentMapper.updateByPrimaryKeySelective(comment);
    }
}
